package com.example.sbbsec;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by Arat.Wu on 2018/12/04.
 */
public class FuctionalMethods4 {



    public static void main(String[] args) {
        final List<City> cities = newArrayList();
        cities.add(City.builder().id(1L).name("太仓").info("太仓信息").build());
        cities.add(City.builder().id(2L).name("沙溪").build());

        final List<City> cities1 = Arrays.asList(City.builder().id(1L).name("太仓").build(),
                City.builder().id(2L).name("沙溪").info("沙溪信息").build(),
                City.builder().id(3L).name("浏河").info("浏河信息").build());

        List<Integer> list3 = Arrays.asList(64, 49, 25, 16, 9, 4, 1, 81, 36, 36);


//        final Optional<City> reduce = cities1.stream().reduce((x, y) -> {
//            x.setId(x.getId() + y.getId());
//            return x;
//        });

        List<City> sorted = cities1.stream().sorted(Comparator.comparing(City::getId).reversed()).peek(System.out::println).collect(Collectors.toList());

        List<City> sorted2 = cities1.stream().sorted(Comparator.comparing(City::getId)).peek(System.out::println).collect(Collectors.toList());

        cities.addAll(cities1);
        final Map<Long, City> collect = cities.stream().collect(Collectors.toMap(City::getId, y -> y, (v1, v2) -> v1));
        final Map<Long, Long> collect2 = cities.stream().collect(Collectors.
                groupingBy(City::getId,
                        Collectors.counting()));
        final Map<Long, List<City>> collect3 = cities.stream().collect(Collectors.
                groupingBy(City::getId,
                        Collectors.toList()));

        final Map<Long, String> collect4 = cities.stream()
                .collect(Collectors.groupingBy(City::getId, Collector.of(StringBuilder::new, (x, y) -> x.append(y.getName()),
                        StringBuilder::append,
                        StringBuilder::toString)));

        final Map<Long, Long> collect5 = cities.stream()
                .collect(Collectors.groupingBy(City::getId, Collectors.reducing(0L, City::getId, Long::sum)));


        /**
         * 参数传递问题
         * 如下
         * private static Long addOne(Long b){
         *     return b+1L;
         * }
         * Long a=0L;
         * addOne(a);
         * System.out.println(a); a=0
         */
        final Map<Long, Long> collect7 = cities.stream()
                .collect(Collectors.groupingBy(City::getId,
                        Collector.of(() ->0L,
                                (a, t) -> a = a+ t.getId(),
                                (a, b) -> {
                                    a= a + b;
                                    return a;
                                },
                                a -> a))
                );

        final Map<Long, Long> collect6 = cities.stream()
                .collect(Collectors.groupingBy(City::getId,
                        Collector.of(() -> new Long[]{0L},

                                (a, t) -> a[0] = a[0] + t.getId(),
                                (a, b) -> {
                                    a[0] = a[0] + b[0];
                                    return a;
                                },
                                a -> a[0]))
                );

        final Map<Long, String> collect1 = cities.stream().collect(Collectors.
                groupingBy(City::getId,
                        Collector.of(StringBuilder::new, StringBuilder::append,
                                (r1, r2) -> {
                                    r1.append(r2);
                                    return r1;
                                },
                                StringBuilder::toString)
                ));


        list3.sort(Integer::compareTo);


    }


}
